package com.choosefine.it.worker;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.choosefine.it.basedata.model.UserInfo;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.json.JsonResult;
import com.choosefine.it.common.json.JsonUtil;
import com.choosefine.it.team.dao.TeamPublishFindWorkerInfoMapper;
import com.choosefine.it.team.service.TeamPublishFindWorkerInfoService;
import com.choosefine.it.worker.controller.WorkerCenterController;
import com.choosefine.it.worker.model.FindWork;
import com.choosefine.it.worker.model.dto.WorkerInfoDTO;
import com.choosefine.it.worker.service.WorkerCenterService;

/**
 * 工人角色-我的中心业务-单元用力测试
 * 
 * 主要作用在于  - 测试推动开发，并测试业务逻辑
 * 
 * @注： 如果有直接请求别人的URL，请先放下，或你们可以依赖我的实现下方式
 * 
 * @author liaozhanggen 2016年12月26日
 */
@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes = Application.class)
//@WebIntegrationTest("server.port:8080")
public class WorkerCenterControllerTestCase {
	@InjectMocks
	TeamPublishFindWorkerInfoService teamPublishFindWorkerInfoService;
	@Mock
	TeamPublishFindWorkerInfoMapper teamPublishFindWorkerInfoMapper;
    @Mock
	private WorkerCenterService workerCenterService;

	private WorkerCenterController workerCenterController;

	@Before
	public void setUp(){
		MockitoAnnotations.initMocks(this);
		workerCenterController = new WorkerCenterController(workerCenterService);
	}
	
	/**
	 * 工人角色-发布找工作成功场景
	 * 
	 * @author liaozhanggen 2016年12月27日
	 */
	@Test
	public void should_worker_publish_work_success() throws BusinessException{
		//准备测试数据
		FindWork findWorkDto = initFindWork();
		
		//mock对象替代 ，返回预期的一结果值
		when(workerCenterService.insertPublishWork(findWorkDto)).thenReturn(1);
		
		//调用
		String message = workerCenterController.publishWork(findWorkDto);
		
		JsonResult msg = JsonUtil.jsonToInstance(message, JsonResult.class);
		assertEquals(ResultCode.MSG_SUCCESS, msg.getMessage());
		
//		如果要测试Service层的Spring MVC如下可用
//		
//		//将调用的workerCenterMapper.insertPublishWork 预期返回期望的值
//		when(workerCenterMapper.insertPublishWork(findWorkDto)).thenReturn(1);
//		
//		//调用业务服务层
//		int num = workerCenterService.insertPublishWork(findWorkDto);
//		
//		//断言是否同预期的一样
//		assertTrue( num > 0 );
	}
	
	/**
	 * 工人角色-发布找工作失败场景
	 * 
	 * @author liaozhanggen 2016年12月27日
	 */
	@Test
	public void should_worker_publish_work_error() throws BusinessException{
		//准备测试数据
		FindWork findWorkDto = initFindWork();
		
		//mock对象替代 ，返回预期的一结果值
		when(workerCenterService.insertPublishWork(findWorkDto)).thenReturn(0);
		
		//调用
		String message = workerCenterController.publishWork(findWorkDto);
		
		JsonResult msg = JsonUtil.jsonToInstance(message, JsonResult.class);
		assertEquals(ResultCode.MSG_ERROR, msg.getMessage());
	}
	
	@Test
	public void should_success() throws BusinessException{
		//准备测试数据
		//mock对象替代 ，返回预期的一结果值
		when(workerCenterService.updateUserInfo(Mockito.any(UserInfo.class))).thenReturn(1);
		
		WorkerInfoDTO workerInfoDTO=new WorkerInfoDTO();
		workerInfoDTO.setContactAddress("2222");
		workerInfoDTO.setPhoneNumber("3333");
		workerInfoDTO.setSkillsCcie("4444");
		workerInfoDTO.setUserCode("123321");
		//调用
		String message = workerCenterController.modifyUserInfo(workerInfoDTO);
		
		JsonResult msg = JsonUtil.jsonToInstance(message, JsonResult.class);
		assertEquals(ResultCode.MSG_SUCCESS, msg.getMessage());
	}
	/** 
	 * @Description：工人首页-找工作-找工作-搜索
	 * @Author：yuxin
	 * @Create Date：2017/2/23/0023
	 * @param：
	 * @return：
	 */
	/*@Test
	public void should_queryTeamRecruitmentWork_success()throws Exception{
		Page<TeamRecruitmentInfo> page=new Page<>();
		when(teamPublishFindWorkerInfoMapper.randomSearch(Mockito.anyString())).thenReturn(page);
		Page<TeamRecruitmentInfo> page1=teamRecruitmentInfoServiceImpl.randomSearch("asasd",1,1);
		*//*page.equals(page1);*//*
		assertEquals(page,page1);
	}*/

	/**
	 * 初始化数据
	 * 
	 * @author liaozhanggen 2016年12月26日
	 */
	private FindWork initFindWork() {
		FindWork findWorkDto = new FindWork();
/*		findWorkDto.setAreaCode("10000,10001|10000,20002");
		findWorkDto.setAreaName("金华|杭州");*/
		findWorkDto.setExpectSalary(new BigDecimal(200));
		findWorkDto.setFirstPhone("13692251785");
		findWorkDto.setUserCode("abc1000");
		findWorkDto.setUserName("张三");
		findWorkDto.setWorkTypeId("3");
		findWorkDto.setSettleType("1");
		return findWorkDto;
	}
}